﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Data;
using Framework;
using System.Data;

namespace GridView
{
    public class CutomGridView : System.Web.UI.WebControls.GridView
    {

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CssClass += "mGrid";
            DisplayBoundData();
        }
        private void DisplayBoundData()
        {
            this.RowDataBound += new GridViewRowEventHandler(OnAddRow);
            DataTable dt = new DataTable();
            IList<Data.VOImage> imgs = ImageData.GetAllImages2();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("Description", typeof(string));

            foreach (VOImage img in imgs)
            {
                DataRow row = dt.Rows.Add();
                row["ID"] = MappingTool.CatchIntNull(img.Id);
                row["Title"] = MappingTool.CatchNull(img.Title);
                row["Description"] = MappingTool.CatchNull(img.Description);
            }

            this.DataSource = dt;
            this.DataBind();
        }

        protected void OnAddRow(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.CssClass += "mainrow" + e.Row.DataItemIndex.ToString();
                TableCell detailedCell = new TableCell();
                Panel pnlDetail = new Panel();
                pnlDetail.CssClass = "detailRow";
                detailedCell.Controls.Add(pnlDetail);
                int colSpan = this.Columns.Count;
                detailedCell.Attributes["ColSpan"] = colSpan.ToString();
                GridViewRow gRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
                // add the cells to the gridviewrow
                gRow.Cells.Add(detailedCell);
                gRow.CssClass += "detailRowInvisible detail" + e.Row.DataItemIndex;
                Table tbl = (Table)e.Row.Parent;
                //if (Convert.ToDouble(e.Row.DataItemIndex + 1) == 1.0) // div nb pages
                //{
                    tbl.Controls.AddAt(this.Controls[0].Controls.Count, gRow);
                //}
            }
        }

        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);
        }
    }
}